﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="DatePicker.ascx.cs" Inherits="controls_DatePicker" %>
<%@ Register TagPrefix="ucc" Assembly="web-controls" Namespace="CodeWork.WebControls" %>

<div class="row">
    <div class="col">
        <ucc:MonthDropDownList ID="uccMonth" runat="server" CssClass="<%=DropDownCssClass %>"></ucc:MonthDropDownList>
    </div>
    <div class="col">
        <asp:TextBox ID="tbxDay" runat="server" Text="Day" CssClass="<%=TextBoxCssClass %>" 
            MaxLength="2" Width="40px" ></asp:TextBox>
    </div>
    <div class="col">
        <asp:TextBox ID="tbxYear" runat="server" Text="Year" CssClass="<%=TextBoxCssClass %>" 
            MaxLength="4" Width="60px" ></asp:TextBox>
    </div>
    <div class="col smallNote">
        (e.g. March 04 2004)&nbsp;
    </div>
    <div class="col">
        <asp:CustomValidator ID="cuvDateValidator" runat="server" ClientValidationFunction="validateDate"
            ErrorMessage="<%$ Code:CodeWork.Library.CommonMessage.DateValidationErrorMessage %>" 
            OnServerValidate="ValidateDate" ControlToValidate="tbxYear" >
        </asp:CustomValidator>
    </div>
</div>

<script src="../javascripts/prototype.js" type="text/javascript"></script>
<script src="../javascripts/ApplicationCommon.js" type="text/javascript"></script>

<script type="text/javascript">
    function validateDate(sender, e)
    {
        var month = Number($('<%= uccMonth.ClientID %>').value);
        var day = Number($('<%= tbxDay.ClientID %>').value);
        var year = Number($('<%= tbxYear.ClientID %>').value);
        var daysOfMonth = new Array(13);
        daysOfMonth[0] = 0;
        daysOfMonth[1] = 31;    // Jan
        daysOfMonth[2] = 28;    // Feb
        daysOfMonth[3] = 31;    // Mar
        daysOfMonth[4] = 30;    // Apr
        daysOfMonth[5] = 31;    // May
        daysOfMonth[6] = 30;    // Jun
        daysOfMonth[7] = 31;    // Jul
        daysOfMonth[8] = 31;    // Aug
        daysOfMonth[9] = 30;    // Sep
        daysOfMonth[10] = 31;   // Oct
        daysOfMonth[11] = 30;   // Nov
        daysOfMonth[12] = 31;   // Dec
        
        if(isNaN(month))
        {
            e.IsValid = false;
            return;
        }
        if(isNaN(year))
        {
            e.IsValid = false;
            return ;
        }
        if(isNaN(day))
        {
            e.IsValid = false;
            return;
        }
        if(month < 1 || month > 12)
        {
            e.IsValid = false;
            return;
        }
        if (day < 1 || day > 31)
        {
            e.IsValid = false;
            return;
        }
        if (year < 1753 || year > 9999)
        {
            e.IsValid = false;
            return;
        }

        if(month == 2)
        {
            if(isLeapYear(year))
            {
                // if the year is leap year and the month 
                // is february day should be withing 29
                if(day > 29)
                    e.IsValid = false;
                else
                    e.IsValid = true;
            }
            else
            {
                if(day > daysOfMonth[month])
                    e.IsValid = false;
                else
                    e.IsValid = true;
            }
        }
        else
        {
            if(day > daysOfMonth[month])
                e.IsValid = false;
            else
                e.IsValid = true;
        }
    }
</script>
